package club.beimeng.eduservice.service.impl;

import club.beimeng.eduservice.entity.CoursePublish.CoursePublishVo;
import club.beimeng.eduservice.entity.EduCourse;
import club.beimeng.eduservice.entity.EduCourseDescription;
import club.beimeng.eduservice.entity.vo.CourseInfoQuery;
import club.beimeng.eduservice.entity.vo.CourseQuery;
import club.beimeng.eduservice.mapper.EduCourseMapper;
import club.beimeng.eduservice.service.EduChapterService;
import club.beimeng.eduservice.service.EduCourseDescriptionService;
import club.beimeng.eduservice.service.EduCourseService;
import club.beimeng.eduservice.service.EduVideoService;
import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-25
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService descriptionService;


    //添加课程基本信息
    @Override
    public String saveCourseInfo(CourseInfoQuery infoQuery) {
        //1 向课程表添加课程基本信息
        // infoQuery转化为eduCourse
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(infoQuery,eduCourse);

        int insert = baseMapper.insert(eduCourse);
        if (insert <= 0){
            System.out.println("添加课程信息失败");
            throw new RuntimeException();
        }

        //获取添加之后的课程id
        String cid = eduCourse.getId();

        //2 向课程简介表添加课程简介
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setDescription(infoQuery.getDescription());
        eduCourseDescription.setId(cid); //因为这里的id是相同的，每个实体类可以自动生成，但是也可以赋值
        descriptionService.save(eduCourseDescription);

        return cid;
    }

    @Override
    public CourseInfoQuery getCourseInfo(String courseId) {

        //查询两个表，返回内容
        EduCourse eduCourse = baseMapper.selectById(courseId);
        EduCourseDescription courseDescription = descriptionService.getById(courseId);

        //返回结果类
        CourseInfoQuery answerCourse = new CourseInfoQuery();
        BeanUtils.copyProperties(eduCourse,answerCourse);
        BeanUtils.copyProperties(courseDescription,answerCourse);

        return answerCourse;
    }

    @Override
    public void updateCourseInfo(CourseInfoQuery courseInfo) {
        // 1 修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        int result = baseMapper.updateById(eduCourse);
        if (result == 0){
            throw new RuntimeException("课程信息修改失败");
        }

        // 2 修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfo,courseDescription);
        descriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        //调用mapper
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseQuery courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null){
            baseMapper.selectPage(pageParam, queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String teacherId = courseQuery.getTeacherId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String subjectId = courseQuery.getSubjectId();
        if (!StringUtils.isEmpty(title)) {
            queryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(teacherId) ) {
            queryWrapper.eq("teacher_id", teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            queryWrapper.ge("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            queryWrapper.ge("subject_id", subjectId);
        }
        baseMapper.selectPage(pageParam, queryWrapper);
    }

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @Override
    public boolean removeCourseById(String id) {
        //根据id删除所有视频
        videoService.removeByCourseId(id);
        //根据id删除所有章节
        chapterService.removeByCourseId(id);
        Integer result = baseMapper.deleteById(id);
        return result > 0;
    }


}
